package chatBot.config;

import chatBot.filter.JwtTokenFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

/**
 * SpringSecurity配置类
 * @author FX
 */
@EnableWebSecurity
@Configuration
public class SecurityConfig {


    @Resource
    JwtTokenFilter jwtAuthenticationTokenFilter;


    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        //禁止csrf防护
        http.csrf().disable().cors();
        //基于token, 不需要session
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        http.authorizeRequests()
                .antMatchers("/gpt/login","/gpt/register","/gpt/sendMsg", "/gpt/wxlogin",
                        "/gpt/logout",
                        "/doc.html",
                        "/webjars/**",
                        "/swagger-resources",
                        "/gpt/checkUserState",
                        "/gpt/commodities",
                        "/gpt/createOrder",
                        "/gpt/bind",
                        "/gpt/notify",
                        "/v2/api-docs").permitAll()

                //除了上面放行的请求 ，所有请求都需要鉴权
                .anyRequest().authenticated();
        // 在中间插入一个Filter
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
        return http.build();
    }

}
